package com.ujoin.dao.imp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ujoin.bean.Tag;
import com.ujoin.dao.ITagDAO;

/**
 * A data access object (DAO) providing persistence and search support for Tag
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.ujoin.bean.Tag
 * @author MyEclipse Persistence Tools
 */

public class TagDAO extends HibernateDaoSupport implements ITagDAO {
	private static final Logger log = LoggerFactory.getLogger(TagDAO.class);
	// property constants
	public static final String TAG_NAME = "tagName";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ITagDAO#save(com.ujoin.bean.Tag)
	 */
	@Override
	public void save(Tag transientInstance) {
		log.debug("saving Tag instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ITagDAO#delete(com.ujoin.bean.Tag)
	 */
	@Override
	public void delete(Tag persistentInstance) {
		log.debug("deleting Tag instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ITagDAO#findById(java.lang.Integer)
	 */
	@Override
	public Tag findById(java.lang.Integer id) {
		log.debug("getting Tag instance with id: " + id);
		try {
			Tag instance = (Tag) getHibernateTemplate().get(
					"com.ujoin.bean.Tag", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ITagDAO#findByExample(com.ujoin.bean.Tag)
	 */
	@Override
	public List findByExample(Tag instance) {
		log.debug("finding Tag instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ITagDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Tag instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Tag as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ITagDAO#findByTagName(java.lang.Object)
	 */
	@Override
	public List findByTagName(Object tagName) {
		return findByProperty(TAG_NAME, tagName);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ITagDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all Tag instances");
		try {
			String queryString = "from Tag";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ITagDAO#merge(com.ujoin.bean.Tag)
	 */
	@Override
	public Tag merge(Tag detachedInstance) {
		log.debug("merging Tag instance");
		try {
			Tag result = (Tag) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ITagDAO#attachDirty(com.ujoin.bean.Tag)
	 */
	@Override
	public void attachDirty(Tag instance) {
		log.debug("attaching dirty Tag instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ITagDAO#attachClean(com.ujoin.bean.Tag)
	 */
	@Override
	public void attachClean(Tag instance) {
		log.debug("attaching clean Tag instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ITagDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ITagDAO) ctx.getBean("TagDAO");
	}
}